package groupon.service;


import groupon.domain.LoginForm;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

public class LoginValidator implements Validator{

	private final static Log log = LogFactory.getLog(LoginValidator.class);
	public boolean supports(Class<?> arg0) {
		return LoginForm.class.equals(arg0);
	}
	//validate Form(Login) from Object is Login
	public void validate(Object obj, Errors error) {
		LoginForm loginForm = (LoginForm)obj;
		boolean flag = false;
		if(loginForm == null){
			error.rejectValue("username",  "error.not-specified", null, "Value required.");
		}else{
			if(StringUtils.isEmpty(loginForm.getUsername())){
				error.rejectValue("username","error.not-specified", null, "Value required.");
				flag = true;
			}	
			if(StringUtils.isEmpty(loginForm.getPassword())){
				error.rejectValue("password","error.not-specified", null, "Value required.");
				flag = true;
			}
		}
		if(!flag){
			if(!loginForm.getUsername().equals("hieu.ta") || !loginForm.getPassword().equals("pyco123A")){
				error.rejectValue("formError","error.not-specified", null, "username or password is incorrect");
			}	
		}
	}
}
